package com.aidex.system.service.impl;

import com.aidex.common.annotation.DataScope;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.domain.entity.SysRole;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.exception.BizException;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sql.SqlUtil;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.common.SysErrorCode;
import com.aidex.system.domain.SysPost;
import com.aidex.system.domain.SysUserPost;
import com.aidex.system.domain.SysUserRole;
import com.aidex.system.domain.vo.StatisticSysUserVo;
import com.aidex.system.mapper.*;
import com.aidex.system.service.ISysUserService;
import com.aidex.system.service.SysConfigService;
import com.aidex.wxCompany.WxWrokMessageService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    // 某些岗位不发送企业微信信息
    private static final String UNSEND_WXMSG_POST_CODE = "sg";

    @Autowired(required = false)
    private SysRoleMapper roleMapper;

    @Autowired(required = false)
    private SysDeptMapper deptMapper;

    @Autowired(required = false)
    private SysPostMapper postMapper;

    @Autowired(required = false)
    private SysUserRoleMapper userRoleMapper;

    @Autowired(required = false)
    private SysUserPostMapper userPostMapper;

    @Autowired(required = false)
    private SysConfigService configService;

    @Autowired(required = false)
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired(required = false)
    private RedisCache redisCache;

    @Autowired(required = false)
    private WxWrokMessageService wxWrokMessageService;

    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public PageInfo<SysUser> findPage(SysUser sysUser) {

        return super.findPage(sysUser);
    }

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        return mapper.selectUserByUserName(userName, BaseEntity.DEL_FLAG_NORMAL);
    }


    /**
     * 查询用户所属角色组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserRoleGroup(String userName) {
        List<SysRole> list = roleMapper.selectRolesByUserName(BaseEntity.DEL_FLAG_NORMAL, userName);
        StringBuffer idsStr = new StringBuffer();
        for (SysRole role : list) {
            idsStr.append(role.getRoleName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString())) {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    /**
     * 查询用户所属岗位组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserPostGroup(String userName) {
        List<SysPost> list = postMapper.selectPostsByUserName(userName);
        StringBuffer idsStr = new StringBuffer();
        for (SysPost post : list) {
            idsStr.append(post.getPostName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString())) {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户名称
     * @return 结果
     */
    @Override
    public void checkUserNameUnique(SysUser user) {
        SysUser sysUserUnique = new SysUser();
        sysUserUnique.setNotEqualId(user.getId());
        sysUserUnique.setUserName(user.getUserName());
        if (!CollectionUtils.isEmpty(mapper.findListWithUnique(sysUserUnique))) {
            throw new BizException(SysErrorCode.B_SYSUSER_UserNameAlreadyExist);
        }
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public void checkPhoneUnique(SysUser user) {
        if (StringUtils.isNotBlank(user.getPhonenumber())) {
            SysUser sysUserUnique = new SysUser();
            sysUserUnique.setNotEqualId(user.getId());
            sysUserUnique.setPhonenumber(user.getPhonenumber());
            if (!CollectionUtils.isEmpty(mapper.findListWithUnique(sysUserUnique))) {
                throw new BizException(SysErrorCode.B_SYSUSER_PhoneAlreadyExist);
            }
        }
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public void checkEmailUnique(SysUser user) {
        if (StringUtils.isNotBlank(user.getEmail())) {
            SysUser sysUserUnique = new SysUser();
            sysUserUnique.setNotEqualId(user.getId());
            sysUserUnique.setEmail(user.getEmail());
            if (!CollectionUtils.isEmpty(mapper.findListWithUnique(sysUserUnique))) {
                throw new BizException(SysErrorCode.B_SYSUSER_EmailAlreadyExist);
            }
        }
    }

    /**
     * 校验用户是否允许操作
     *
     * @param user 用户信息
     */
    @Override
    public void checkUserAllowed(SysUser user) {
        if (StringUtils.isNotNull(user.getId()) && user.isAdmin()) {
            throw new CustomException("不允许操作超级管理员用户");
        }
    }

    /**
     * 新增保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public boolean insertUser(SysUser user) {
        checkUserNameUnique(user);
        checkPhoneUnique(user);
        checkEmailUnique(user);
        if (StringUtils.isBlank(user.getPassword())) {
            String password = ConfigUtils.getConfigValueByKey("sys.user.initPassword", "123456");
            user.setPassword(password);
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        //user.setUserPinyin(PinYin4JCn.getFullAndSimplePinYin(user.getName(), 500));

        // 新增用户信息
        boolean result = super.save(user);
        // 新增用户岗位关联
        insertUserPost(user);
        // 新增用户与角色管理
        insertUserRole(user);
        // 获取企业微信用户ID
        insertWxUserId(user);
        return result;
    }

    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public boolean updateUser(SysUser user) {
        checkUserNameUnique(user);
        checkPhoneUnique(user);
        checkEmailUnique(user);
        String userId = user.getId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPostByUserId(userId);
        // 新增用户与岗位管理
        insertUserPost(user);
        // 获取企业微信用户ID
        this.insertWxUserId(user);
        // 如果离职 客户和进件交接
        if ("1".equals(user.getStatus())) {
            SysUser toUser = mapper.get(user.getToUserId());
            if (toUser != null) {
                user.setToUserName(toUser.getName());
            }
            mapper.updateCustomerToUser(user);
            mapper.updateJinjianToUser(user);
        }


        // 同步更新客户相关的部门信息
        if (StringUtils.isNotNull(user.getDeptId())) {
            // 校验是否修改了部门
            SysUser savedUser = mapper.get(user.getId());
            if (!user.getDeptId().equals(savedUser.getDeptId())) {
                SysDept sysDept = deptMapper.get(user.getDeptId());
                if (null != sysDept) {
                    user.setDeptName(sysDept.getDeptName());
                }
                // 执行速率较慢 异步执行
                CompletableFuture.runAsync(() -> {
                    mapper.updateCustomerDeptInfo(user);
                });
            }
        }
        redisCache.setCacheObject("sysUserId:" + userId, user);
        boolean result = super.save(user);
        // 强退用户
        Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
        for (String key : keys) {
            LoginUser redisUser = redisCache.getCacheObject(key);
            if (null != redisUser && redisUser.getUserId().equals(user.getUserId())) {
                redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + redisUser.getToken());
            }
        }
        return result;
    }

    /**
     * 修改用户状态
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updateUserStatus(SysUser user) {
        return super.save(user);
    }

    /**
     * 修改用户基本信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updateUserProfile(SysUser user) {
        return super.save(user);
    }

    /**
     * 记录登录信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updateUserLoginInfo(SysUser user) {
        return mapper.updateUserLoginInfo(user) > 0;
    }

    /**
     * 修改用户头像
     *
     * @param userName 用户名
     * @param avatar   头像地址
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updateUserAvatar(String userName, String avatar) {
        return mapper.updateUserAvatar(userName, avatar) > 0 ? true : false;
    }

    /**
     * 重置用户密码
     *
     * @param userName 用户名
     * @param password 密码
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int resetUserPwd(String userName, String password) {
        return mapper.resetUserPwd(userName, password);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    @Override
    @Transactional(readOnly = false)
    public void insertUserRole(SysUser user) {
        String[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (String roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                userRoleMapper.batchUserRole(list);
            }
        }
    }

    /**
     * 新增用户岗位信息sy
     *
     * @param user 用户对象
     */
    @Override
    @Transactional(readOnly = false)
    public void insertUserPost(SysUser user) {
        String[] posts = user.getPostIds();
        if (StringUtils.isNotNull(posts)) {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (String postId : posts) {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0) {
                userPostMapper.batchUserPost(list);
            }
        }
    }

    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean deleteUserById(String userId) {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 删除用户与岗位表
        userPostMapper.deleteUserPostByUserId(userId);
        return remove(new SysUser(userId));
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteUserByIds(String[] userIds) {
        for (String userId : userIds) {
            checkUserAllowed(new SysUser(userId));
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPost(userIds);
        // 去公海
        mapper.deleteUsersToGongHai(userIds);

        // 删除分配规则
        mapper.deleteRuleByIds(userIds, BaseEntity.DEL_FLAG_DELETE, BaseEntity.DEL_FLAG_NORMAL);
        // 删除分配人员
        mapper.deleteRuleUserByIds(userIds, BaseEntity.DEL_FLAG_DELETE, BaseEntity.DEL_FLAG_NORMAL);
        //删除规则临时表
        mapper.deleteTempRuleByIds(userIds);
        //删除人员临时表
        mapper.deleteTempRuleUserByIds(userIds);
        return mapper.deleteUserByIds(userIds, BaseEntity.DEL_FLAG_DELETE);
    }

    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysUser user : userList) {
            try {
                // 根据部门编码查询
                SysDept dept = deptMapper.getDeptByCode(user.getDeptCode());
                user.setDeptId(dept.getId());
                // 根据岗位名称查询岗位ID
                SysPost post = postMapper.getPostByName(user.getPostName());
                String postId = post.getId();
                String str[] = {postId};
                user.setPostIds(str);
                // 根据角色名称查询角色Id
                SysRole role = roleMapper.getRoleByName(user.getRoleName());
                String roleId = role.getId();
                String roles[] = {roleId};
                user.setRoleIds(roles);
                // 验证是否存在这个用户
                SysUser u = mapper.selectUserByUserName(user.getUserName(), BaseEntity.DEL_FLAG_NORMAL);
                if (StringUtils.isNull(u)) {

                    if (StringUtils.isBlank(user.getPassword())) {
                        String password = ConfigUtils.getConfigValueByKey("sys.user.initPassword", "123456");
                        user.setPassword(password);
                    }
                    user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
                    //user.setUserPinyin(PinYin4JCn.getFullAndSimplePinYin(user.getName(), 500));
                    // 新增用户信息
                    super.save(user);
                    // 新增用户岗位关联
                    insertUserPost(user);
                    // 新增用户与角色管理
                    insertUserRole(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                } else if (isUpdateSupport) {
                    user.setUpdateBy(operName);
                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Map<String, Object>> getUserInfoByIds(JSONObject userIdsObj) {
        String[] userIds = userIdsObj.getString("userIds").split(";");
        int i = 0;
        //考虑mybatis foreach的限制，所以定义参数格式为list内还是list
        List<List<String>> idsList = new ArrayList<List<String>>();
        List<String> idList = new ArrayList<String>();
        for (String userId : userIds) {
            //当id个数超出限制后，则新new一个list来存放
            if (i % 500 == 0 && i > 0) {
                idsList.add(idList);
                idList = new ArrayList<String>();
            }
            idList.add(userId);
            i++;
        }
        idsList.add(idList);
        return mapper.getUserInfoByIds(idsList);
    }

    /**
     * 根据用户ID查询用户的角色信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public PageInfo<SysUser> findRoleUserPage(SysUser sysUser) {
        PageDomain page = sysUser.getPage();
        PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderByColumn());
        return new PageInfo(mapper.findRoleUserList(sysUser));
    }

    /**
     * 写入用户角色信息
     *
     * @param sysUserRole
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean insertRoleUser(SysUserRole sysUserRole) {
        String[] userIds = sysUserRole.getUserId().split(";");
        String roleId = sysUserRole.getRoleId();
        int i = 0;
        //考虑mybatis foreach的限制，所以定义参数格式为list内还是list
        List<List<String>> idsList = new ArrayList<List<String>>();
        List<String> idList = new ArrayList<String>();
        for (String userId : userIds) {
            //当id个数超出限制后，则新new一个list来存放
            if (i % 500 == 0 && i > 0) {
                idsList.add(idList);
                idList = new ArrayList<String>();
            }
            idList.add(userId);
            i++;
        }
        idsList.add(idList);
        List<Map<String, Object>> resultList = sysUserRoleMapper.getRoleUserInfoByIds(idsList, roleId);
        String userUserIds = "";
        if (!CollectionUtils.isEmpty(resultList)) {
            for (Map<String, Object> obj : resultList) {
                userUserIds += obj.get("user_id") + ";";
            }
        }
        List<SysUserRole> userRoleList = new ArrayList<SysUserRole>();
        for (String userId : userIds) {
            System.out.println(userId + "===" + userUserIds.indexOf(userId));
            if (userUserIds.indexOf(userId) < 0) {
                SysUserRole insertSysUserRole = new SysUserRole();
                insertSysUserRole.setRoleId(roleId);
                insertSysUserRole.setUserId(userId);
                userRoleList.add(insertSysUserRole);
            }
        }
        if (!CollectionUtils.isEmpty(userRoleList)) {
            sysUserRoleMapper.batchUserRole(userRoleList);
        }
        return true;
    }

    /**
     * 注册用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean registerUser(SysUser user) {
        return mapper.insert(user) > 0;
    }

    @Override
    public List<SysUser> findListByCompanyId(SysUser sysUser) {
        return mapper.findListByCompanyId(sysUser);
    }

    @Override
    @Transactional(readOnly = false)
    public void resetDayShareCount() {
        mapper.resetDayShareCount();
    }

    @Override
    @Transactional(readOnly = false)
    public Integer updateDayShareCount(SysUser sysUser) {
        return mapper.updateDayShareCount(sysUser);
    }

    @Override
    public List<SysUser> findShareUser(Map<String, Object> params) {
        return mapper.findShareUser(params);
    }

    @Override
    public List<SysUser> findUserByCity(Map<String, Object> params) {
        return mapper.findUserByCity(params);
    }

    @Override
    public Integer findMaxSort(SysUser sysUser) {
        return NumberUtils.nextOrder(mapper.findMaxSort(sysUser));
    }

    @Override
    @Transactional(readOnly = false)
    public Integer updateUserUpdateCustomer() {
        return mapper.updateUserUpdateCustomer();
    }

    @Override
    public PageInfo<StatisticSysUserVo> statisticsUserByLaiyuan(SysUser sysUser) {
        PageDomain page = sysUser.getPage();
        PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderByColumn());
        return new PageInfo(mapper.statisticsUserByLaiyuan(sysUser));
    }

    @Override
    public List<StatisticSysUserVo> statisticsUserByLaiyuanList(SysUser sysUser) {
        return mapper.statisticsUserByLaiyuan(sysUser);
    }

    @Override
    public SysUser getUserCache(String userId) {
        SysUser sysUser = redisCache.getCacheObject("sysUserId:" + userId);
        if (StringUtils.isNotNull(sysUser)) {
            return sysUser;
        } else {
            sysUser = mapper.get(userId);
            redisCache.setCacheObject("sysUserId:" + userId, sysUser);
            return sysUser;
        }

    }

    @Override
    @Transactional(readOnly = false)
    public Map<String, String> checkWxUserId(SysUser user) {
        if (StringUtils.isNotNull(user.getPhonenumber())) {
            Map<String, Object> result = wxWrokMessageService.getUserIdByPhone(user.getPhonenumber());
            Map<String, String> checkMap = new HashMap<>();
            if ((boolean) result.get("isSuccess")) {
                checkMap.put("code", "1");
                checkMap.put("wxUserId", result.get("userId").toString());
                // 同步到用户表
                user.setUserPinyin(result.get("userId").toString());
                this.updateUserPinyin(user);
            } else {
                checkMap.put("code", "2");
                checkMap.put("errmsg", result.get("errmsg").toString());
            }
            return checkMap;
        }
        return null;
    }


    public boolean insertWxUserId(SysUser user) {

        //查询岗位
        boolean containsSg = false;
        List<SysPost> list = postMapper.selectPostsByUserName(user.getUserName());
        if (!list.isEmpty()) {
            containsSg = list.stream().anyMatch(post -> UNSEND_WXMSG_POST_CODE.equals(post.getPostCode()));
        }
        if (StringUtils.isNotNull(user.getPhonenumber()) && StringUtils.isNull(user.getUserPinyin()) && !containsSg) {
            Map<String, Object> result = wxWrokMessageService.getUserIdByPhone(user.getPhonenumber());
            if ((boolean) result.get("isSuccess")) {
                // 同步到用户表
                user.setUserPinyin(result.get("userId").toString());
                return this.updateUserPinyin(user) > 0;
            }
        }
        return false;
    }


    @Override
    @Transactional(readOnly = false)
    public int updateUserPinyin(SysUser sysUser) {
        return mapper.updateUserPinyin(sysUser);
    }

    @Override
    public List<SysUser> findSendWxMsgUserListByIds(String[] ids) {
        // 某些岗位不发送信息
        String[] unSendPostCodes = new String[]{UNSEND_WXMSG_POST_CODE};
        return mapper.findSendWxMsgUserListByIds(ids, unSendPostCodes);
    }

    @Override
    public List<SysUser> findAllSendWxMsgUserList() {
        SysUser sysUser = new SysUser();
        sysUser.setNotInPostCodes(new String[]{UNSEND_WXMSG_POST_CODE});
        return findList(sysUser);
    }

    @Override
    public PageInfo<SysUser> findListByUserIds(SysUser sysUser) {
        PageDomain page = sysUser.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(mapper.findListByUserIds(sysUser));
    }
}











